from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtMultimedia import *

"""视频接口"""
class VideoSurface(QAbstractVideoSurface):
    showImageSignal = pyqtSignal(QImage)

    def __init__(self, parent=None):
        super(VideoSurface, self).__init__(parent)

    def supportedPixelFormats(self, type):
        return [QVideoFrame.Format_RGB32, QVideoFrame.Format_RGB32]

    def present(self, frame):
        """获取帧并发送信号"""
        if frame.isValid():
            cloneFrame = QVideoFrame(frame)
            cloneFrame.map(QAbstractVideoBuffer.ReadOnly)
            img = QImage(cloneFrame.bits(), cloneFrame.width(), cloneFrame.height(),
                         QVideoFrame.imageFormatFromPixelFormat(cloneFrame.pixelFormat()))
            cloneFrame.unmap()
            self.showImageSignal.emit(img)
            return True
        return False

